package com.jackrain.clouddata.oms.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.jackrain.clouddata.oms.util.KeyValueUtils.keyAppend;
import static com.jackrain.clouddata.oms.util.KeyValueUtils.valueAppend;

/**
 * Created by zcy  2017/8/15.
 */
@Slf4j
@Component
public class MPurDao {

    @Transactional
    public void execute(JSONObject mainJson, JSONArray itemArray, Long clientId) {
        log.info("=====MPurDao=======");
        String countSql = "SELECT COUNT(1) FROM M_PUR WHERE AD_CLIENT_ID = ? AND BILLNO = ?";
        int count = QueryEngine.getInstance().doQueryInt(countSql, new Object[]{clientId, mainJson.getString("BILLNO")});

        if (count == 0) {
            for (int i = 0; i < itemArray.size(); i++) {
                JSONObject itemJson = itemArray.getJSONObject(i);
                //获取字表名称
                String itemName = itemJson.getString("name");
                //获取value
                JSONArray valueArray = itemJson.getJSONArray("value");
                if (valueArray != null && valueArray.size() > 0) {
                    JSONArray valuesA = new JSONArray();
                    for (int j = 0; j < valueArray.size(); j++) {
                        JSONObject valueJson = JSON.parseObject(JSON.toJSONString(valueArray.getJSONObject(j), new SerializerFeature[]{SerializerFeature.WriteMapNullValue}), Feature.OrderedField);
                        for (int o = 0; o < valueJson.keySet().size(); o++) {
                            String key = (String) valueJson.keySet().toArray()[o];
                            String mpuritemvalue = valueJson.getString(key);
                            if (null == mpuritemvalue) {
                                valueJson.put(key, "");
                            }
                            if (key.contains("DATE")) {
                                valueJson.put(key, TypeUtils.castToDate(valueArray.getJSONObject(j).get(key)));
                            }
                        }
                        valueJson.put("AD_CLIENT_ID", clientId);
                        valueJson.put("ID", Tools.getSequence(itemName));
                        valuesA.set(j, valueJson);
                    }
                    StringBuffer saleitemInsertSql = new StringBuffer();
                    saleitemInsertSql.append("INSERT INTO M_PURITEM (" + keyAppend(valuesA.getJSONObject(0).keySet()) + ") VALUES(").append(valueAppend(valuesA.getJSONObject(0).size())).append(")");

                    QueryEngine.getInstance().executeBatchUpdate(saleitemInsertSql.toString(), valuesA);

                }
            }

            JSONObject saleJson = JSON.parseObject(JSON.toJSONString(mainJson, new SerializerFeature[]{SerializerFeature.WriteMapNullValue}), Feature.OrderedField);
            for (int i = 0; i < saleJson.keySet().size(); i++) {
                String key = (String) saleJson.keySet().toArray()[i];
                if (key.contains("DATE")) {
                    saleJson.put(key, TypeUtils.castToDate(mainJson.get(key)));
                }
            }
            saleJson.put("AD_CLIENT_ID", clientId);

            saleJson.put("ID", Tools.getSequence("M_PUR"));
            StringBuffer saleInsertSq = new StringBuffer();
            saleInsertSq.append("INSERT INTO M_PUR(" + keyAppend(saleJson.keySet()) + ") VALUES(").append(valueAppend(saleJson.size())).append(")");
            String saleInsertSql = saleInsertSq.toString();
            for (int j = 0; j < saleJson.keySet().size(); j++) {
                String key = (String) saleJson.keySet().toArray()[j];
                String value = saleJson.getString(key);
                if (value == null) {
                    if (!key.contains("DATE")) {
                        saleJson.put(key, "");
                    } else {
                        saleJson.remove(key);
                        saleInsertSql = saleInsertSql.replace(key + ",", "");
                    }
                }
            }
            QueryEngine.getInstance().executeUpdate(saleInsertSql.toString(), saleJson.values().toArray());
        }

    }

}
